package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("Event4reportController")
public class Event4reportController extends CmiEmaController {

	@RequestMapping("/listEvent4reports")
	public ModelAndView listEvent4reports(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = event4reportPaginationService.updatePagination(request, pagination);
		Set<Event4report> event4reports = event4reportPaginationService.getPaginationEvent4reports(pagination);

		mav.addObject("event4reports", event4reports);
		mav.addObject("entityName", "event4report");
		mav.addObject("pagination", pagination);
		mav.setViewName("event4report/listEvent4reports.jsp");
		return mav;
	}

	public String listEvent4reports() {
		return "redirect:/listEvent4reports";
	}

	@RequestMapping("/viewEvent4report")
	public ModelAndView viewEvent4report(@RequestParam Integer event4reportId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("event4report", event4reportDAO.findEvent4reportByPrimaryKey(event4reportId));
		mav.setViewName("event4report/viewEvent4report.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEvent4reports")
	public ModelAndView confirmDeleteEvent4reports(
			@RequestParam(value = "deleteEvent4reportsIds", required = true) Set<Integer> deleteEvent4reportsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Event4report> deleteEvent4reports = event4reportDAO.findEvent4reportsByIds(deleteEvent4reportsIds);
		Set<Event4report> referencedEvent4reports = event4reportService.loadReferenceEvent4reports(deleteEvent4reports);
		boolean hasReferenced;
		if (referencedEvent4reports.size() == 0) {
			mav.addObject("deleteEntitys", deleteEvent4reports);
			hasReferenced = false;
		} else {
			deleteEvent4reports.removeAll(referencedEvent4reports);
			mav.addObject("referencedEntitys", referencedEvent4reports);
			mav.addObject("deleteEntitys", deleteEvent4reports);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Event4report");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEvent4reports");
		return mav;
	}

	@RequestMapping("/deleteEvent4report")
	public String deleteEvent4report(@RequestParam Integer event4reportId) {
		Event4report event4report = event4reportDAO.findEvent4reportByPrimaryKey(event4reportId);
		event4reportService.deleteEvent4report(event4report);
		return "forward:/listEvent4reports";
	}

	@RequestMapping("/confirmDeleteEvent4report")
	public ModelAndView confirmDeleteEvent4report(@RequestParam Integer event4reportId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("event4report", event4reportDAO.findEvent4reportByPrimaryKey(event4reportId));

		mav.setViewName("event4report/deleteEvent4report.jsp");

		return mav;
	}

	@RequestMapping("/deleteEvent4reports")
	public String deleteEvent4reports(
			@RequestParam(value = "deleteEvent4reportsId", required = true) Set<Integer> deleteEvent4reportsId) {
		event4reportService.deleteEvent4reports(deleteEvent4reportsId);
		return "forward:/listEvent4reports";
	}

	@RequestMapping("/editEvent4report")
	public ModelAndView editEvent4report(@RequestParam Integer event4reportId) {

		ModelAndView mav = new ModelAndView();

		Event4report event4report = event4reportDAO.findEvent4reportByPrimaryKey(event4reportId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("event4report", event4report);

		// 把所有的引用event4report对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event4reportService.loadEvent4reportObjectReferencedSet(event4report));

		// 将编辑页面event4report添加到mav中
		mav.setViewName("event4report/editEvent4report.jsp");
		return mav;
	}

	@RequestMapping("/newEvent4report")
	public ModelAndView newEvent4report() {
		ModelAndView mav = new ModelAndView();
		Event4report event4report = new Event4report();

		mav.addObject("event4report", event4report);

		// 把所有的引用event4report对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(event4reportService.loadEvent4reportObjectReferencedSet(event4report));

		mav.addObject("newFlag", true);
		mav.setViewName("event4report/editEvent4report.jsp");

		return mav;
	}

	@RequestMapping("/saveEvent4report")
	public ModelAndView saveEvent4report(@ModelAttribute Event4report event4report) {

		ModelAndView mav = new ModelAndView();

		Event4report existingEvent4report = CmiHibernateProxyUtil
				.deproxy(event4reportDAO.findEvent4reportByPhysicalPrimaryKey(event4report.getName()));

		// 新建记录
		if (event4report.getId() == null) {
			if (event4report.equals(existingEvent4report)) {

				mav.addAllObjects(event4reportService.loadEvent4reportClassReferencedSet(event4report));
				// mav = editContinue(mav, event4report);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event4report/editEvent4report.jsp");
			} else {
				event4reportService.saveEvent4report(event4report);
				mav.setViewName("forward:/listEvent4reports");
			}
		}
		// 编辑记录
		else {
			Event4report originEvent4report = CmiHibernateProxyUtil
					.deproxy(event4reportDAO.findEvent4reportByPrimaryKey(event4report.getId()));
			if (!event4report.equals(originEvent4report) && event4report.equals(existingEvent4report)) {
				mav.addAllObjects(event4reportService.loadEvent4reportClassReferencedSet(event4report));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("event4report/editEvent4report.jsp");
			} else {
				event4reportService.saveEvent4report(event4report);
				mav.setViewName("forward:/listEvent4reports");
			}
		}
		return mav;
	}

	@RequestMapping("/event4reportController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
